<template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-center">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent label-width="80px" label-position="left">
          <el-col :span="6">
            <el-form-item label="仓库名称">
            <el-input v-model="listQuery.fstockname" clearable class="filter-item" placeholder="请输入仓库名称" />
          </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="所在位置">
              <el-select v-model="listQuery.fparentNo" clearable class="filter-item" placeholder="请选择所在位置">
                <el-option
                  v-for="item in eqtActionOptions"
                  :key="item.fpointNo"
                  :label="item.fpointName"
                  :value="item.fpointNo" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item class="text" label="仓库类型" prop="ftype">
              <el-select v-model="listQuery.ftype" class="filter-item" clearable>
                <el-option
                  v-for="item in ftypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button
              type="primary"
                @click="handleFilter">查找</el-button>
              <el-button
              type="success"
                @click="handleCreate">添加</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
        <JNPF-table
      v-loading="listLoading"
      :data="list">
      <el-table-column :width="buttonColor.operationTwo" align="center" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <el-button type="text" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column :width="buttonColor.line" :index="indexMethod" align="center" label="序号" type="index" />
      <el-table-column :width="buttonColor.billno" align="left" label="仓库编号" prop="fstockno" />
      <el-table-column :width="buttonColor.name" align="left" label="仓库名称" prop="fstockname" />
      <el-table-column :formatter="formatfparentNo" align="left" label="所在位置" prop="fparentNo" />
      <el-table-column align="left" label="备注" prop="fmemo" />
    </JNPF-table>
    <paginations
	:total="total"
	:page.sync="listQuery.currentPage"
	:limit.sync="listQuery.pageSize"
	/>
      </div>
    </div>
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="50%">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent label-width="80px" ref="dataForm" :rules="rules" :model="dataForm" label-position="left">
          <el-col :span="12">
            <el-form-item class="required" label="仓库编号" prop="fstockno">
              <el-input v-if="dialogStatus == 'create'" v-model="dataForm.fstockno" />
              <el-input v-else v-model="dataForm.fstockno" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item class="required" label="客户名称" prop="fsiteNo">
              <el-select
                v-model="dataForm.fsiteNo"
                disabled
                clearable
                filterable
                class="filter-item"
                placeholder="请选择客户名称">
                <el-option v-for="item in getListfsiteNo" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item class="required" label="仓库名称" prop="fstockname">
              <el-input v-model="dataForm.fstockname" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item class="required" label="所在位置" prop="fparentNo">
              <el-select v-model="dataForm.fparentNo" clearable class="filter-item" placeholder="请选择所在位置">
                <el-option
                  v-for="item in eqtActionOptions"
                  :key="item.fpointNo"
                  :label="item.fpointName"
                  :value="item.fpointNo" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="责任人" prop="fperson">
              <el-select
                  v-model="dataForm.fperson"
                  style="display: inline-block"
                  clearable
                  filterable
                  class="filter-item"
                  placeholder="请选择责任人">
                  <el-option v-for="item in getListfperson" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
             </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="当前状态" prop="fpointState">
              <el-select v-model="dataForm.fpointState" class="filter-item" clearable>
                <el-option
                  v-for="item in fpointStateOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item class="text required" label="仓库类型" prop="ftype">
              <el-select v-model="dataForm.ftype" class="filter-item" clearable>
                <el-option
                  v-for="item in ftypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="Number(item.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item class="text" style="width:99%" label="备注" prop="fmemo">
              <el-input v-model="dataForm.fmemo" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
             <el-form-item class="text" style="width:99%" label="视频URL" prop="fvideoUrl">
              <el-input v-model="dataForm.fvideoUrl" />
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button
          @click="dialogFormVisible = false">取消</el-button>
        <el-button
          v-if="dialogStatus == 'create'"
          type="primary"
          @click="createData">确定</el-button>
        <el-button
          v-else
           type="primary"
          @click="updateData">确定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { listtsStock, createtsStock, updatetsStock, deletetsStock } from '@/api/tsStock'
import { fsiteOptionsByLevel, getListArea } from '@/api/area'
import { listPointByFsiteNo2 } from '@/api/airAlarm'
import Pagination from '@/components/Pagination'
import UserDialog from '@/components/UserDialog'
import moment from 'moment'
import { tableColor, titleColor, buttonColor } from '@/filters/index'

const fpointStateMap = {
  0: '禁用',
  1: '启用'
}

export default {
  name: 'ITsStock',
  components: { Pagination, UserDialog },
  filters: {
    fpointStateFilter(type) {
      return fpointStateMap[type]
    }
  },
  data() {
    return {
      ftypeList: [], // 类型
      buttonColor,
      eqtActionOptions: [],
      getListfperson: [],
      getListfsiteNo: [],
      searchVisibleuse: false,
      userDialogFsiteNo: null,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        fstockno: undefined,
        fstockname: undefined,
        fparentNo: undefined,
        fsiteNo: this.$store.state.user.fsiteNo,
        fmemo: undefined,
        fvideoUrl: undefined,
        fperson: undefined,
        fpointState: undefined,
        fupddate: undefined,
        foperator: undefined,
        ftype: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      tableColor: tableColor,
      titleColor: titleColor,
      dataForm: {
        fstockno: undefined,
        fstockname: undefined,
        fparentNo: undefined,
        fsiteNo: undefined,
        fmemo: undefined,
        fvideoUrl: undefined,
        fperson: undefined,
        fpointState: undefined,
        fupddate: undefined,
        foperator: undefined,
        ftype: undefined,
        id: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        fstockno: [
          { required: true, message: '仓库编号不能为空' }
        ],
        fstockname: [
          { required: true, message: '仓库名称不能为空' }
        ],
        fparentNo: [
          { required: true, message: '所在位置不能为空' }
        ],
        fsiteNo: [
          { required: true, message: '客户名称不能为空' }
        ],
        ftype: [
          { required: true, message: '仓库类型不能为空' }
        ],
        fpointState: [
          { type: 'number', message: '状态必须为数字值' }
        ]
      },
      fpointStateOptions: [
        { value: 1, label: '正常' },
        { value: 0, label: '删除' }
      ]
    }
  },
  created() {
    this.getList()
    fsiteOptionsByLevel(3)
      .then(response => {
        this.getListfsiteNo = response.data.data.list
      })
      .catch(() => {
        this.getListfsiteNo = []
      })

    // 人员弹出框
    getListArea({ flag: 13, arg1: this.$store.state.user.fsiteNo })
      .then(response => {
        this.getListfperson = response.data.data.list
      })
      .catch(() => {
        this.getListfperson = []
      })

    listPointByFsiteNo2(this.$store.state.user.fsiteNo)
      .then(response => {
        this.eqtActionOptions = response.data.data.list
      })
      .catch(() => {
        this.eqtActionOptions = []
      })
      // 类型
    getListArea({ flag: 2017 })
      .then(response => {
        this.ftypeList = response.data.data.list
      })
      .catch(() => {
        this.ftypeList = []
      })
  },
  methods: {
    datetimeFormat(row, column) {
      const date = row[column.property]
      if (date === undefined || date === null) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    // 人员弹出框
    searchDialoguse() {
      this.searchVisibleuse = true
      this.userDialogFsiteNo = this.dataForm.fsiteNo
    },

    // 人员弹出框返回值
    userDialogValue(value) {
      this.dataForm.fperson = value.user.id.toString()
    },

    // 所在位置
    formatfparentNo(row) {
      if (this.eqtActionOptions.length > 0) {
        for (let i = 0; i < this.eqtActionOptions.length; i++) {
          if (row.fparentNo == this.eqtActionOptions[i].fpointNo) {
            return this.eqtActionOptions[i].fpointName
          }
        }
        return ''
      } else {
        return ''
      }
    },

    getList() {
      this.listLoading = true
      listtsStock(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        fstockno: undefined,
        fstockname: undefined,
        fparentNo: undefined,
        fsiteNo: undefined,
        fmemo: undefined,
        fvideoUrl: undefined,
        fperson: undefined,
        fpointState: undefined,
        fupddate: undefined,
        foperator: undefined,
        ftype: undefined,
        id: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.dataForm.fsiteNo = this.$store.state.user.fsiteNo
      this.dataForm.fperson = this.$store.state.user.id.toString()
      this.dataForm.fpointState = this.fpointStateOptions[0].value
      this.dataForm.ftype = 1
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createtsStock(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加物料仓库成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.data
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updatetsStock(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.fstockno === this.dataForm.fstockno) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新物料仓库成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.data
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除物料仓库', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletetsStock(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除物料仓库成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.data
            })
          })
      }).catch(() => { })
    },
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>